package com.lagou.edu.config;

import com.lagou.edu.TransportIPFilter;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        System.out.println("拦截器执行");
        String remoteAddr = request.getRemoteAddr();
        //通过ThreadLocal传递
        TransportIPFilter.THREAD_LOCAL.set(remoteAddr);
        //通过RpcContext传递
        RpcContext.getContext().setAttachment("controllerRemoteAddr", remoteAddr);

        return true;
    }
}
